﻿package pw.archipelag.loaderGame.panel {
	import pw.archipelag.loaderGame.panel.scores.MessageScoreOperation;
	import pw.archipelag.loaderGame.configVars.ConfigVarsFromToAs2;
	import base.loader.URLLoaderExt;
	import pw.archipelag.loaderGame.interfaceGame.skinsPanel.SkinScores;
	import pw.archipelag.loaderGame.panel.scores.ScrollScores;
	import base.loader.LoadContentQueue;
	import flash.events.Event;
	import base.bridgeAs2As3.BridgeFromToAs2Event;
	import com.adobe.crypto.MD5;
	import pw.archipelag.loaderGame.Config;
	import base.types.ArrayExt;
	import base.dspObj.DoOperations;
	import pw.archipelag.loaderGame.interfaceGame.skinsPanel.SkinTf;
	
	public class PanelScores extends PanelAbstractLabel {
		
		private var scorePoints: int;
		private var scorePercent: int;
		private var messageScoreOperation: MessageScoreOperation;
		private var configVarsFromToAs2: ConfigVarsFromToAs2;
		private var urlLoaderExtResults: URLLoaderExt;
		private var skinScores: SkinScores;
		private var scrollScores: ScrollScores;
		private var _width: Number;
		private var _height: Number;
		private var resultSaveLessMoreNewScoreError: int;
		
		public function PanelScores(scorePoints: int = -1, scorePercent: int = -1): void {
			this.scorePoints = scorePoints;
			this.scorePercent = scorePercent;
			super("NAJLEPSI");
			this.createMessageScoreOperation();
			this.loadConfigVars();
		}
		
		private function createMessageScoreOperation(): void {
			this.messageScoreOperation = new MessageScoreOperation();
			this.addChild(this.messageScoreOperation);
		}
	
		private function loadConfigVars(): void {
			var loadContentQueue: LoadContentQueue = new LoadContentQueue(this.printAndSaveOrGetScores, this);
			loadContentQueue.addToLoadQueue(Config.PATH_LOADER_GAME + "configVars.swf");
			loadContentQueue.startLoading();
		}
		
		private function printAndSaveOrGetScores(arrGame: Array): void {
			this.resultSaveLessMoreNewScoreError = -1;
			if ((this.scorePoints > -1) && (Config.DESCRIPTION_PLAYER))	this.printAndSaveScore();
			else this.getScores();
		}
		
		private function printAndSaveScore(): void {
			this.messageScoreOperation.addEventListener(Event.CLOSE, this.saveScore);
			this.messageScoreOperation.showAndCloseScoreMessage(this.scorePoints, this.scorePercent);
			this.drawMessageScoreOperation();
		}
		
		private function saveScore(e: Event): void {
			this.messageScoreOperation.removeEventListener(Event.CLOSE, this.saveScore);
			this.messageScoreOperation.showDurationMessage(0);
			this.drawMessageScoreOperation();
			this.configVarsFromToAs2 = new ConfigVarsFromToAs2(this.scorePoints, this.scorePercent);
			this.configVarsFromToAs2.addEventListener(BridgeFromToAs2Event.EXIT, this.callbackFromBridgeAs2As3SaveScore);
			/*var hashScoreUrl: String = MD5.hash(Config.ID_SESSION + String(this.scorePoints) + String(this.scorePercent));
			var arrParams: Array = [["hash", hashScoreUrl]];
			var urlLoaderExt: URLLoaderExt = new URLLoaderExt();
			urlLoaderExt.sendAndLoad(Config.URL_SERVICE + "saveresult/" + Config.ID_SESSION + "/" + this.scorePoints + "/" + this.scorePercent, arrParams, [this.callbackSaveScore], 0, 60000, false);*/		
		}
		
		private function callbackSaveScore(isLoaded: Boolean, nodeResultSave: String, ...args): void {
			this.callbackFromBridgeAs2As3SaveScore(new BridgeFromToAs2Event(BridgeFromToAs2Event.EXIT, {nodeResultSave: nodeResultSave, isLoaded: isLoaded}));
		}	
			
		private function callbackFromBridgeAs2As3SaveScore(e: BridgeFromToAs2Event): void {
			this.configVarsFromToAs2.removeEventListener(BridgeFromToAs2Event.EXIT, this.callbackFromBridgeAs2As3SaveScore);
			this.configVarsFromToAs2 = null;
			this.resultSaveLessMoreNewScoreError = 3;
			//trace(e.data.isLoaded, e.data.nodeResultSave)
			if (e.data.isLoaded) {
				var xmlResultSave: XML = new XML(e.data.nodeResultSave);
				var resultSave: * = xmlResultSave[0];
				if ((resultSave != "") && (!isNaN(uint(resultSave)))) this.resultSaveLessMoreNewScoreError = uint(resultSave);
			}
			this.getScores();
		}
		
		private function getScores(): void {
			this.messageScoreOperation.showDurationMessage(1);
			this.drawMessageScoreOperation();
			this.urlLoaderExtResults = new URLLoaderExt();
			this.urlLoaderExtResults.sendAndLoad(Config.URL_SERVICE + "gettopscores/" + Config.ID_SESSION, [], [this.callbackGetScores], 1, 60000, false);
		}
		
		private function callbackGetScores(isLoaded: Boolean, nodeScores: String, ...args): void {
			this.urlLoaderExtResults = null;
			if ((this.scorePoints > -1) && (!Config.DESCRIPTION_PLAYER)) this.resultSaveLessMoreNewScoreError = 4;
			this.messageScoreOperation.showAndCloseLoadSaveMessage(uint(isLoaded), this.resultSaveLessMoreNewScoreError);
			if (isLoaded) {
				this.parseXMLScores(nodeScores);
				this.drawScrollScores();
			}
		}
		
		private function parseXMLScores(nodeScores: String): void {
			nodeScores = nodeScores.replace(new RegExp("xmlns[^\"]*\"[^\"]*\"", "gi"), "");
			var xmlScores: XML = new XML(nodeScores);
			var xmlListScore: XMLList = new XMLList(xmlScores.*);
			var arrObjResult: ArrayExt = new ArrayExt();
			for each (var xmlScore: XML in xmlListScore)
				arrObjResult.push({lp: Number(xmlScore.Place), login: String(xmlScore.Nick), scorePoints: Number(xmlScore.Result), scorePercent: Number(xmlScore.ResultPercent)});
			arrObjResult.sortOn("lp", [Array.NUMERIC]);
			this.scrollScores = new ScrollScores(arrObjResult);
			this.scrollScores.y = this.tfLabel.y + this.tfLabel.height + PanelAbstractBg.MARGIN_Y_ELEMENTS;
			this.scrollScores.alpha = 0;
			DoOperations.hideShow(this.scrollScores, 1);
			this.addChild(this.scrollScores);
		}
		
		public function draw(skinTfLabelTitles: SkinTf, skinScores: SkinScores): void {
			this.drawToTfLabel(skinTfLabelTitles);
			this.skinScores = skinScores;
		}
			
		private function drawMessageScoreOperation(): void {
			this.messageScoreOperation.draw(this.skinScores.skinTfMessage);
			this.height = this._height;
		}
		
		private function drawScrollScores(): void {
			this.scrollScores.draw(this.skinScores);
			this.width = this._width;
			this.height = this._height;
		}
		
		override public function get width(): Number {
			return this._width;
		}
		
		override public function set width(value: Number): void {
			this._width = value;
			if (this.scrollScores) this.scrollScores.width = value;
			this.messageScoreOperation.width = value;
		}
		
		override public function get height(): Number {
			return this._height;
		}
		
		override public function set height(value: Number): void {
			this._height = value;
			if (this.scrollScores)
				this.scrollScores.height = value /*- PanelAbstractBg.MARGIN_Y_ELEMENTS / 2*/ - this.messageScoreOperation.height - this.scrollScores.y;
			if (this.messageScoreOperation.hasEventListener(Event.CLOSE))
				this.messageScoreOperation.y = (value - this.messageScoreOperation.height) / 2;
			else this.messageScoreOperation.y = value - this.messageScoreOperation.height /*- PanelAbstractBg.MARGIN_Y_ELEMENTS / 2*/;
		}
		
		override public function destroy(): void {
			if (this.messageScoreOperation.hasEventListener(Event.CLOSE)) this.messageScoreOperation.removeEventListener(Event.CLOSE, this.saveScore);
			if (this.configVarsFromToAs2) {
				this.configVarsFromToAs2.removeEventListener(BridgeFromToAs2Event.EXIT, this.callbackFromBridgeAs2As3SaveScore);
				this.configVarsFromToAs2 = null;
			}
			if (this.urlLoaderExtResults) {
				this.urlLoaderExtResults.destroy();
				this.urlLoaderExtResults = null;
			}
			this.messageScoreOperation.destroy();
			if (this.scrollScores) this.scrollScores.destroy();
			super.destroy();
		}
		
	}
	
}